<?php
error_reporting(E_ALL);

$start = (isset($_GET['start']) && is_numeric($_GET['start'])) ? $_GET['start'] : 1;
$show  = (isset($_GET['show'])  && is_numeric($_GET['show']))  ? $_GET['show'] : 10;

echo "<head>
<title>eAthena Bug Reports</title>
</head>

<body>

<table>
<tr><td><b>Bug ID</b></td><td><b>Summary</b></td><td><b>Status</b></td><td><b>Branch</b></td><td><b>Component</b></td></tr>
";

for ($bug = $start; $bug < ($start+$show); $bug++) {
	$page = file_get_contents("http://www.eathena.ws/board/index.php?autocom=bugtracker&showbug=" . $bug);
	if (preg_match('#The error returned was:</h4>[^<]*<p>([^<]*)</p>#', $page, $matches)) {
		switch ($matches[1]) {
		case 'You do not have permission to view this area!':
			$summary = '<font color="#FF0000">NEED TO LOGIN MANUALLY</font>';
			break;
		case 'Incorrect use of one of the board files':
			$summary = '<font color="#FF0000">(bug report does not exist)</font>';
			break;
		}
		$status = "";
		$branch = "";
		$project = "";
	} else if (preg_match('#Bug ID \#([0-9]*)#', $page)) {
		preg_match('#<title>(.*) - Bug Tracker</title>#', $page, $matches);
		$summary = $matches[1];
		
		preg_match('#<strong>Status:</strong></td>[^<]*<td class=\'row2\' width=\'40%\' align=\'left\'>([^<]*)</td>#', $page, $matches);
		$status = str_replace("\n(", "(", trim($matches[1]));
		
		preg_match('#<strong>Branch:</strong></td>[^<]*<td class=\'row2\' width=\'40%\' align=\'left\'>([^<]*)</td>#', $page, $matches);
		$branch = trim($matches[1]);
		
		preg_match('#<strong>Project:</strong></td>[^<]*<td class=\'row2\' width=\'10%\' style=\'border-right:1px solid black\' align=\'left\'>([^<]*)</td>#', $page, $matches);
		$project = trim($matches[1]);
	}
	echo sprintf("<tr><td><a href=\"http://www.eathena.ws/board/index.php?autocom=bugtracker&showbug=%d\" target=\"_blank\">%d</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", 
	              $bug,$bug, $summary, $status, $branch, $project);
}

echo sprintf("<tr><td><a href=\"?start=%d&show=%d\">PREV</a></td><td></td><td><a href=\"?start=%d&show=%d\">NEXT</a></td><td></td><td></td></tr>\n", 
             ($start-$show), $show, 
             ($start+$show), $show);

echo "</table>
<br />

</body>
</html>
";
exit;
?>
